$(function () {
    //声明变量
    var memberClient_form = $("#memberClient_form");
    var memberClient_datagrid =   $("#memberClient_datagrid");
    var memberMin_datagrid =   $("#memberMin_datagrid");

    //右侧会员信息表格
    memberMin_datagrid.datagrid({
        url:'/member/query.do',
        title:"会员信息表",
        fitColumns:true,
        width:850,
        height:313,
        rownumber:true,
        singleSelect:true,
        columns:[[
            {field:'id',checkbox:true},
            {field:'cardNumber',title:'会员卡号',width:80},
            {field:'name',title:'会员姓名',width:80},
            {field:'levelName',title:'会员等级',width:80},
            {field:'birthday',title:'会员生日',width:80},
            {field:'address',title:'地址',width:80},
            {field:'groupName',title:'分组类型',width:80},
            {field:'points',title:'积分',width:80}
        ]]
    });

    //下面会员明细表格
        memberClient_datagrid.datagrid({
        url:'/memberClient/query.do',
        //fit:true,
        title:"会员充值信息表",
        fitColumns:true,
        pagination:true,
        width:1350,
        height:250,
        rownumber:true,
        singleSelect:true,
        columns:[[
            {field:'id',checkbox:true},
            {field:'cardNumber',title:'卡号',width:80},
            {field:'payment',title:'操作类型',width:80},
            {field:'operation',title:'充值方式',width:80},
            {field:'amount',title:'充值前总金额',width:80},
            {field:'recharge',title:'充值金额',width:80},
            {field:'presenter',title:'赠送金额',width:80},
            {field:'totalAmount',title:'卡上余额',width:80},
            {field:'rechargeDate',title:'充值时间',width:80},
            {field:'remark',title:'备注',width:80}
        ]],
        //点击一行数据的时候触发
        onClickRow:function (index,row) {
            if(row){
                //获取每一列的值
                var totalAmount=row.totalAmount;

                var rows=memberClient_datagrid.datagrid("getSelected");
                //发送sql去查询会员的所有信息
                $.get("/member/selectByMemberClientId.do",{id:rows.id},function (data) {
                    //再把对应的值填充到表单中去
                    $("#cardNumber").textbox("setValue",data.cardNumber);
                    $("#name").textbox("setValue",data.name);
                    $("#levelName").textbox("setValue",data.levelName);
                    $("#birthday").textbox("setValue",data.birthday);
                    $("#point").textbox("setValue",data.points);
                    $("#total").textbox("setValue",data.total); //累计消费
                    $("#amount").textbox("setValue",totalAmount);  //充值前费用
                    $("#idd").val(rows.id); //保存更新的时候, 传递过去
                    //查询消费总额, 然后设置到表格中
                    $.get("/member/selectByTotalAmount.do",{id:row.id},function (data) {
                        $("#total").val(data);
                    })
                });
            }
        }
    });

    //充次操作
    $("#rechargeTime").click(function () {
       window.location.href="/rechargeTime/view.do";
    });


    //统一函数
    var methodObj = {
        //充值的时候, 提交表单
        recharge:function (){
            //判断是否选择了一条数据
            var row =memberClient_datagrid.datagrid("getSelected");
            if(row){
                $.messager.confirm("确认对话框","确认要充值吗?",function (r) {
                        if(r) {
                            //提交表单
                            memberClient_form.form("submit", {
                                url: '/memberClient/saveOrUpdate.do',
                                success: function (data) {
                                    //回调函数:data表示返回数据解析为json
                                    data = $.parseJSON(data);
                                    if (data.success) {
                                        //如果返回结果成功__弹出窗口提示
                                        $.messager.alert('温馨提示', '充值成功', 'info', function () {
                                            memberClient_datagrid.datagrid('reload');
                                            $("#memberClient_form").form("clear");

                                        })

                                    } else {
                                        //如果保存失败,则弹出窗口提示
                                        $.messager.alert('温馨提示', data.msg, 'error');
                                    }
                                }
                            });
                        }})
            }else{
                $.messager.alert('温馨提示',"请选择一条数据",'error');
                return;
            }
        },
        query:function (){
            //获取查询条件
            var keyword =  $("#keyword").textbox('getValue');
            //通过datagrid 的 load方法传递参数
            memberClient_datagrid.datagrid('load',{
                keyword:keyword
            });
        }};

    //统一绑定事件:
    $("a[data-cmd]").click(function () {
        var methodname = $(this).data("cmd");
        //动态获取调用方法名字,调用方法;
        methodObj[methodname]();
    });

    //合计充值金额
    $("input[name='recharge'],input[name='presenter']").blur(function () {
        var td=$(this).closest("td");
        var recharge=td.find("[name='recharge']").val() || 0;
        var presenter=td.find("[name='presenter']").val() || 0;
        var sum=parseFloat(presenter)+parseFloat(recharge);
        //把和放入充值列中
        $("#totalAmount").html(sum);
    });


});

